
from ..models import Model

import time


class ResPartner(Model):
    _name = 'res.partner'
    _metadata = {
        "context": {
            "default_company_id": lambda self: self.allowed_company_id,
        },

        'fieldsForSearch': {
            'name': None,
            "company_id": "1",
            "type": "1",
            "property_payment_term_id": "",
        },

        'fieldsForBrowse': {
            'name': None,
            "type": "1",
            "property_payment_term_id": "",
        },

        "field_onchange": {
            # "property_payment_term_id": "",
            # "property_supplier_payment_term_id": "",
            # "property_account_position_id": "",
            # "company_id": "1",
            # "property_account_receivable_id": "",
            # "property_account_payable_id": "",
            "company_id": "1",
            "parent_id": "1",
            "name": "1",
            "active": "1",
            "type": "1",
            "is_company": "1",
            "company_type": "1",
            "lang": "1",
            "category_id": "",
            "customer_rank": "",
            "supplier_rank": "", },

        "import_export": {
            'model': 'res.partner',
            'fields': ["name", "is_company", "parent_id", "type",
                       "street", "zip", "city",
                       "state_id", "country_id"],

            'name_create_enabled_fields': {},

            'fields_export':  [
                {"name": "id", "label": "外部ID"},
                {"name": "phone", "label": "电话"},
                {"name": "company_type", "label": "公司类别"},
                {"name": "email", "label": "Email"},
                {"name": "user_id", "label": "销售员"},
                {"name": "activity_ids", "label": "活动"},
                {"name": "city", "label": "城市"},
                {"name": "country_id", "label": "国家"},
                {"name": "company_id", "label": "公司"}
            ]},

        'columnsForPage': {
            'company_id': {'type': 'select', 'label': '公司'},
            'company_type': {'type': 'select', 'label': '公司或个人'},

            'image_128__url': {'type': 'img', 'label': '图片'},
            'image_128': {'type': 'img', 'label': '图片'},
            'name': {'label': '名称'},
            'ref': {'label': '内部编码'},

            'email': {'label': '邮箱'},
            'category_id': {
                'type': 'select',
                'multiple': 'multiple',
                'label': '标签',
                'placeholder': '标签'
            },
            'date': {'type': 'date', 'label': '日期'},
            'type': {'type': 'radio', 'label': '类型'},
            'image_1920': {'type': 'img', 'url': 'image_128__url', 'label': '图片'},
            'comment': {'type': 'textarea', 'label': '备注', 'placeholder': '备注'}
        },
    }


class ResPartnerCompany(ResPartner):
    _name = 'res.partner.company'
    _inherit = 'res.partner'

    _metadata = {
        'fieldsForSearch': {
            'type': None
        },

        "context": {
            # "res_partner_search_mode": "customer",
            # "search_default_customer": 1
            "default_customer_rank": 1,
            "default_supplier_rank": 1,
            "default_is_company": True},


    }


class ResPartnerCompanyCustomer(ResPartnerCompany):
    _name = 'res.partner.company.customer'
    _inherit = 'res.partner.company'

    _metadata = {
        'fieldsForSearch': {
            'email': None
        },
    }


class ResConfig(Model):
    _name = 'res.config.settings'

    def test(self):

        # self.test_set_chart()
        self.test_set_tax()

    def test_set_tax(self):
        session = self.env.session
        print(session)

        user_companies = session['user_companies']
        print(user_companies)

        allowed_companies = user_companies['allowed_companies']
        print(allowed_companies)

        com = allowed_companies[0]

        cid = com[0]
        context = {
            'allowed_company_ids': [cid]
        }
        values = self.default_get_onchange(context=context)

        # chart_template_id = self.env.ref(
        #     'l10n_cn_small_business.l10n_chart_china_small_business')

        # values['chart_template_id'] = chart_template_id

        values['sale_tax_id'] = None
        values['purchase_tax_id'] = None

        for k, v in values.items():
            print(k, ',', v)

        print(time.time(), '--to create')
        res_config_id = self.create(values)

        print(time.time(), '----res_config_id', res_config_id)
        # res = self.read(res_config_id, [])
        # print(time.time(), '----read ok', res_config_id)

        # for k, v in res[0].items():
        #     print(k, ',', v)

        res = self.execute(res_config_id, context=context)
        print('----res_config_id', res)

    def test_set_chart(self):
        session = self.env.session
        print(session)

        user_companies = session['user_companies']
        print(user_companies)

        allowed_companies = user_companies['allowed_companies']
        print(allowed_companies)

        com = allowed_companies[0]

        cid = com[0]
        context = {
            'allowed_company_ids': [cid]
        }
        values = self.default_get_onchange(context=context)

        chart_template_id = self.env.ref(
            'l10n_cn_small_business.l10n_chart_china_small_business')

        values['chart_template_id'] = chart_template_id

        for k, v in values.items():
            print(k, ',', v)

        res_config_id = self.create(values)

        print('----res_config_id', res_config_id)
        res = self.read(res_config_id, [])

        for k, v in res[0].items():
            print(k, ',', v)

        res = self.execute(res_config_id, context=context)
        print('----res_config_id', res)

        pass


class ResCompany(Model):
    _name = 'res.company'

    def test(self):
        self.test_create()
        pass

    def test_create(self):
        fields_onchage = {
            "logo": "",
            "__last_update": "",
            "partner_gid": "",
            "name": "",
            "partner_id": "1",
            "street": "",
            "street2": "",
            "city": "",
            "state_id": "1",
            "zip": "",
            "country_id": "1",
            "country_code": "",
            "phone": "",
            "email": "",
            "website": "",
            "vat": "",
            "company_registry": "",
            "currency_id": "",
            "parent_id": "",
            "sequence": "",
            "favicon": "1"
        }

        vals = self.onchange([], {}, [], fields_onchage)

        # vals = self.default_get_onchange()
        print('-----vals', vals)

        # name, logo
        # name = 'Comp1'
        # vals['name'] = name
        # res = self.create2(vals)
        # print(res)


class IrTranslation(Model):
    _name = 'ir.translation'

    def test(self):
        pass
